package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.domain.SysConsumer;
import com.ruoyi.system.domain.req.SysTranServiceReq;
import com.ruoyi.system.domain.vo.SysUserDto;
import com.ruoyi.system.mapper.SysConsumerMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysTransactionServiceMapper;
import com.ruoyi.system.domain.SysTransactionService;
import com.ruoyi.system.service.ISysTransactionServiceService;

/**
 * 交易服务模板信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-28
 */
@Service
public class SysTransactionServiceServiceImpl implements ISysTransactionServiceService 
{
    @Autowired
    private SysTransactionServiceMapper sysTransactionServiceMapper;

    @Autowired
    private SysConsumerMapper sysConsumerMapper;

    /**
     * 查询交易服务模板信息
     * 
     * @param modualId 交易服务模板信息主键
     * @return 交易服务模板信息
     */
    @Override
    public SysTransactionService selectSysTransactionServiceByModualId(Long modualId)
    {
        return sysTransactionServiceMapper.selectSysTransactionServiceBymoduleId(modualId);
    }

    /**
     * 查询交易服务模板信息列表
     * 
     * @param
     * @return 交易服务模板信息
     */
    @Override
    public List<SysTransactionService> selectSysTransactionServiceList(SysTransactionService sysTransactionService)
    {
        List<SysTransactionService> sysTransactionServiceList =  sysTransactionServiceMapper.selectSysTransactionServiceList(sysTransactionService);

        if(sysTransactionServiceList.size()>0){

            for(SysTransactionService sysTransactionService1 : sysTransactionServiceList){

                //通过销售方式，模板id，模板名称统计供应商数量 将统计的数量值塞到列表字段返回
                SysConsumer sysConsumer = new SysConsumer();
                String saleType= sysTransactionService1.getSaleType();
                sysConsumer.setSaleType(saleType);
                if("1".equals(saleType)){
                    sysConsumer.setBmodualId(sysTransactionService1.getModuleId());
                    sysConsumer.setBmodualName(sysTransactionService1.getModuleName());
                }
                if("2".equals(saleType)){
                    sysConsumer.setCmodualId(sysTransactionService1.getModuleId());
                    sysConsumer.setCmodualName(sysTransactionService1.getModuleName());
                }
                if("3".equals(saleType)){
                    sysConsumer.setBmodualId(sysTransactionService1.getModuleId());
                    sysConsumer.setBmodualName(sysTransactionService1.getModuleName());
                    sysConsumer.setCmodualId(sysTransactionService1.getModuleId());
                    sysConsumer.setCmodualName(sysTransactionService1.getModuleName());
                }
              int result =  sysConsumerMapper.countSupplierTotal(sysConsumer);
                sysTransactionService1.setSuppliers(result);
            }
        }


        return sysTransactionServiceList;
    }

    /**
     * 新增交易服务模板信息
     * 
     * @param sysTransactionService 交易服务模板信息
     * @return 结果
     */
    @Override
    public int insertSysTransactionService(SysTransactionService sysTransactionService)
    {
        return sysTransactionServiceMapper.insertSysTransactionService(sysTransactionService);
    }

    /**
     * 修改交易服务模板信息
     * 
     * @param sysTransactionService 交易服务模板信息
     * @return 结果
     */
    @Override
    public int updateSysTransactionService(SysTransactionService sysTransactionService)
    {
        return sysTransactionServiceMapper.updateSysTransactionService(sysTransactionService);
    }

    /**
     * 批量删除交易服务模板信息
     * 
     * @param modualIds 需要删除的交易服务模板信息主键
     * @return 结果
     */
    @Override
    public int deleteSysTransactionServiceByModualIds(Long[] modualIds)
    {
        return sysTransactionServiceMapper.deleteSysTransactionServiceByModualIds(modualIds);
    }

    /**
     * 删除交易服务模板信息信息
     * 
     * @param modualId 交易服务模板信息主键
     * @return 结果
     */
    @Override
    public int deleteSysTransactionServiceByModualId(Long modualId)
    {
        return sysTransactionServiceMapper.deleteSysTransactionServiceByModualId(modualId);
    }

    @Override
    public List<SysTransactionService> selectSysTransactionServiceListBySaleType(String saleType) {


        SysTransactionService sysTransactionService = new SysTransactionService();
        sysTransactionService.setSaleType(saleType);
        sysTransactionService.setIsEnabled("1");
        return sysTransactionServiceMapper.selectSysTransactionServiceListBySaleType(sysTransactionService);
    }

    /**
     * 修改状态
     * @param moduleId
     * @param isEnabled
     * @return
     */
    @Override
    public int changeStatus(Long moduleId, String isEnabled) {
        SysTransactionService sysTransactionService = new SysTransactionService();
        sysTransactionService.setModuleId(moduleId);
        sysTransactionService.setIsEnabled(isEnabled);
        return sysTransactionServiceMapper.updateSysTransactionService(sysTransactionService);

    }



    /**
     * 查看详情
     * @param sysTransactionService
     * @return
     */
    @Override
    public TableDataInfo viewDetail(SysTransactionService sysTransactionService) {


        //签约供应商明细
        String saleType = sysTransactionService.getSaleType();
        SysConsumer sysConsumer = new SysConsumer();
        sysConsumer.setSaleType(saleType);
        if(saleType.equals("1")){
            sysConsumer.setBmodualId(sysTransactionService.getModuleId());
            sysConsumer.setBmodualName(sysTransactionService.getModuleName());
        }
        if(saleType.equals("2")){
            sysConsumer.setCmodualId(sysTransactionService.getModuleId());
            sysConsumer.setCmodualName(sysTransactionService.getModuleName());
        }
        List<SysConsumer> lists= sysConsumerMapper.selectSysConsumerList(sysConsumer);

        List<SysUserDto> resultList = new ArrayList<>();
        for (SysConsumer sysConsumer1 : lists) {

            SysUserDto sysUserDto = new SysUserDto();
            BeanUtils.copyProperties(sysConsumer1,sysUserDto);
            resultList.add(sysUserDto);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(resultList);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(resultList).getTotal());


        return rspData;
    }

    /**
     * 搜索获取详情信息
     * @param
     * @return
     */
    @Override
    public TableDataInfo getUserList(SysTranServiceReq sysTranServiceReq) {
        SysConsumer sysConsumer = new SysConsumer();

        SysTransactionService tranServiceInfo = sysTransactionServiceMapper.selectSysTransactionServiceBymoduleId(sysTranServiceReq.getModuleId());

        String saleType = tranServiceInfo.getSaleType();

        if(saleType.equals("1")){
            sysConsumer.setBmodualId(tranServiceInfo.getModuleId());
            sysConsumer.setBmodualName(tranServiceInfo.getModuleName());
        }
        if(saleType.equals("2")){
            sysConsumer.setCmodualId(tranServiceInfo.getModuleId());
            sysConsumer.setCmodualName(tranServiceInfo.getModuleName());
        }
        sysConsumer.setOrgId(sysTranServiceReq.getOrgId());
        sysConsumer.setOrgName(sysTranServiceReq.getOrgName());
        List<SysConsumer> lists= sysConsumerMapper.selectSysConsumerList(sysConsumer);
        List<SysUserDto> resultList = new ArrayList<>();
        for (SysConsumer sysConsumer2 : lists) {

            SysUserDto sysUserDto = new SysUserDto();
            BeanUtils.copyProperties(sysConsumer2,sysUserDto);
            resultList.add(sysUserDto);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(resultList);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(resultList).getTotal());

        return rspData;
    }
}
